package com.ljt.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.business.entity.ApplyFor;
import com.ljt.business.entity.ApplyForDetail;
import com.ljt.business.entity.Product;
import com.ljt.business.service.ApplyForDetailService;
import com.ljt.business.service.ApplyForService;
import com.ljt.business.service.ApplyForVoService;
import com.ljt.business.vo.ApplyForVo;
import com.ljt.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ljt
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/applyFor")
@Api(value = "申请模块", tags = "业务申请接口")
@CrossOrigin
public class ApplyForController {

    @Autowired
    private ApplyForVoService applyForVoService;
    @Autowired
    private ApplyForService applyForService;
    @Autowired
    private ApplyForDetailService applyForDetailService;

    @ApiOperation(value = "申请列表", notes = "模糊查询申请信息")
    @GetMapping("/queryAllApplyForByCondition")
    public Result queryAllApplyForByCondition(
            @RequestParam(required = false, defaultValue = "1") Integer current,
            @RequestParam(required = false, defaultValue = "6") Integer size,
            @RequestParam(required = false) String firstTime,
            @RequestParam(required = false) String lastTime,
            @RequestParam(required = false) String status) {

        Page<ApplyForVo> applyForPage = new Page<>(current, size);
        applyForPage = applyForVoService.queryAllApplyForByCondition(applyForPage, firstTime, lastTime, status);
        return Result.ok().data("applyForPage", applyForPage);
    }

    @PostMapping("/insertApplyFor")
    @ApiOperation(value = "添加申请信息", notes = "添加申请")
    public Result insertApplyFor(@RequestBody ApplyFor applyFor) {
        //先添加 申请表 和 申请明细表 的数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            result += random.nextInt(10);
        }
        //获取申请id
        String applyId = "pa" + newDate + result;

        ArrayList<Product> productList = applyFor.getProductList();
        ApplyForDetail applyForDetail = new ApplyForDetail();

        //申请表添加
        applyFor.setApplyId(applyId);//设置申请id
        applyFor.setCreateTime(new Date());//设置申请时间

        boolean i = applyForService.save(applyFor);
        if (i) {
            for (Product p : productList) {
                applyForDetail.setApplyId(applyId);
                applyForDetail.setPid(p.getPNum());
                applyForDetail.setApplyNum(p.getApplyNum());

                //申请明细表添加
                boolean j = applyForDetailService.save(applyForDetail);
                if(!j){
                    return Result.error();
                }
            }
            return Result.ok().data("applyFor", applyFor);
        } else {
            return Result.error();
        }
    }
}
